<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span class="box"
          ><el-input
            style="width: 250px; padding-lift: 3px"
            v-model="queryData.username"
            placeholder="请用户名搜索"
            size="medium"
          ></el-input>
        </span>
        <span>
          <el-button
            style="padding-lift: 3px"
            size="medium"
            @click="resetSearch"
            >清空</el-button
          >
          <el-button size="medium" type="primary" plain @click="initData"
            >搜索</el-button
          >
        </span>
        <el-button
          style="float: right; padding: 3px 0; width: 100px; height: 30px"
          type="success"
          icon="el-icon-edit"
          @click="dialogVisible = true"
          >新增用户</el-button
        >
      </div>
      <br />
       <el-alert
        :title="`共${counts}条记录`"
        type="info"
        show-icon
        :closable="false"
      />
      <br />
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="id" label="序号" width="180" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180" align="center">
        </el-table-column>
        <el-table-column
          prop="phone"
          label="联系电话"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="permission_group_title"
          label="权限组名称"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="role" label="角色" width="180" align="center">
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="editHandler(row.id)"
            ></el-button>
            <el-button
              v-if="row.id !== 2"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delHandler(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryData.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(counts)"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 弹出层 -->
    <el-dialog
      :title="formData.id ? '修改' : '添加'"
      :visible.sync="dialogVisible"
    >
      <el-form
        v-if="dialogVisible"
        ref="form"
        :model="formData"
        :rules="formRules"
        label-width="300px"
      >
        <el-form-item label="用户名" style="width: 500px" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            style="width: 200px"
            v-model="formData.email"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" style="width: 500px" prop="password">
          <el-input v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" style="width: 500px" prop="role">
          <el-input v-model="formData.role" placeholder="请输入角色"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称" prop="title">
          <el-select
            v-model="formData.permission_group_id"
            placeholder="请选择"
          >
            <el-option
              v-for="item in permissionsList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" style="width: 500px" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="介绍" style="width: 500px" prop="introduction">
          <el-input
            type="textarea"
            :rows="2"
            v-model="formData.introduction"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <!-- 按钮 -->
        <el-button @click="dialogVisible = false" size="small">取消</el-button>
        <el-button type="primary" size="small" @click="btnOk">提交</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserListApi,
  getPermissionsApi,
  addUsersApi,
  delUsersApi,
  getUsersInfoApi,
  editUsersApi,
} from "@/api/user";
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      queryData: {
        username: "",
        page: 1,
        pagesize: 10,
      },
      // 权限组要上传的数据
      permissionsData: {
        title: "",
        page: 1,
        pagesize: 10,
      },
      permissionsList: [],
      counts: "",
      // 添加的数据
      formData: {
        username: "", //用户名
        password: "", //密码
        phone: "", //联系电话
        role: "", //角色名称
        permission_group_id: "", //权限组id
        avatar: "", //头像
        email: "", //邮件
        introduction: "",
      },
      formRules: {
        username: [
          { required: true, message: "请输入", trigger: "blur" },
          { min: 2, max: 6, message: "长度在2-6", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            pattern: /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
            message: "邮箱格式不正确",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^[a-z0-9_-]{6,18}$/,
            message: "密码格式不正确，字母开头",
            trigger: "blur",
          },
        ],
<<<<<<< HEAD
        role: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        title: [
          { required: true, message: "请输入权限组名称", trigger: "blur" },
        ],
        phone: [{ required: true, message: "请输入11位电话号码", trigger: "blur" }],
        introduction: [{ required: true, message: "请输入介绍", trigger: "blur" }],
=======
>>>>>>> gxd
      },
    };
  },
  created() {
    this.initData();
    this.permissions();
  },
  methods: {
    // 每页显示数量
    handleSizeChange(val) {
      this.queryData.pagesize = val;
      this.initData();
    },
    // 第几页
    handleCurrentChange(val) {
      this.queryData.page = val;
      this.initData();
    },
    // 获取列表数据
    async initData() {
      let { list, counts } = await getUserListApi(this.queryData);
      // console.log(res);
      this.list = list;
      this.counts = counts;
    },
    // 搜索重置
    resetSearch() {
      this.queryData.username = "";
      this.initData();
    },
    async permissions() {
      let { list } = await getPermissionsApi(this.permissionsData);
      // console.log(res);
      this.permissionsList = list;
    },
    // 添加
    async btnOk() {
      // try {
      //   await this.$refs.form.validate();
      // } catch (error) {
      //   console.log(error);
      // }
      if (this.formData.id) {
        await editUsersApi(this.formData);
        this.$message.success("修改成功");
      } else {
        await addUsersApi(this.formData);
        this.$message.success("添加成功");
      }

      this.formData = {};
      this.dialogVisible = false;
      this.initData();
    },
    // 修改
    async editHandler(id) {
      let res = await getUsersInfoApi(id);
      console.log(res);
      this.formData = res;
      this.dialogVisible = true;
    },
    // 删除
    async delHandler(id) {
      await this.$confirm("确认删除吗");
      await delUsersApi(id);
      this.$message.success("删除成功");
      this.initData();
    },
  },
};
</script>

<<<<<<< HEAD
<style  lang="scss" scoped>
=======
<style>
.box {
  margin-right: 10px;
}
>>>>>>> gxd
</style>
